import http from './http';
import { store } from "../redux/store"
// promise的封装操作
function to(promise) {
    return promise
        .then((data) => {
            if (data.code == 1006) {
                console.log(data.code, "token过期1006-----------------------------")
                store.dispatch((dispatch) => {
                    dispatch({ type: 'app/setIsLogin', payload: true })
                })
            }
            return [null, data];
        })
        .catch((err) => [err]);
}
function returnErr(err) {
    return Object.assign(err, {
        status: "406",
        description: err.errMsg
    }, true);
}
const httpApi = {
    message: "tweetwallet",
    inline: "https://home.tweetwallet.io/upapi/inline/",
    wsapiWs: "wss://tweetwallet.io/wsapi/ws",
    defaultAvatar: "3rPNXPiPe7v7jnPYmUyik8wu6RCuS90_Lt8MJ3uExLo.png",
    // login请求
    async register(params) {
        const url = '/register';
        const [err, res] = await to(http.post(url, params));
        if (err) {
            // return Object.assign(err, {
            //     status: "406",
            //     description: err.errMsg
            // }, true);
            return returnErr(err)
        }
        //请求成功
        return res;
    },
    async login(params) {
        const url = '/login';
        const [err, res] = await to(http.post(url, params));
        if (err) {
            return returnErr(err)
        }
        //请求成功
        return res;
    },
    async verifyToken(params) {
        const url = '/verifyToken';
        const [err, res] = await to(http.get(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async getUserInfo(params) {
        const url = '/getUserInfo';
        const [err, res] = await to(http.get(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },
    async setUserInfo(params) {
        const url = '/setUserInfo';
        const [err, res] = await to(http.post(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async fileExist(params) {
        const url = '/fileExist';
        const [err, res] = await to(http.post(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async setUserNickName(params) {
        const url = '/setUserNickName';
        const [err, res] = await to(http.post(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async setUserProfilePhoto(params) {
        const url = '/setUserProfilePhoto';
        const [err, res] = await to(http.post(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },



    async postComment(params) {
        const url = '/postComment';
        const [err, res] = await to(http.post(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },


    async post(params) {
        const url = '/post';
        const [err, res] = await to(http.post(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async postLike(params) {
        const url = '/postLike';
        const [err, res] = await to(http.post(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async postFavorite(params) {
        const url = '/postFavorite';
        const [err, res] = await to(http.post(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async follow(params) {
        const url = '/follow';
        const [err, res] = await to(http.post(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },


    async getFollowingList(params) {
        const url = '/getFollowingList';
        const [err, res] = await to(http.get(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async getAppVersion(params) {
        const url = '/getAppVersion';
        const [err, res] = await to(http.get(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },


    async getPostListByUserFollow(params) {
        const url = '/getPostListByUserFollow';
        const [err, res] = await to(http.get(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async getPostListByUserId(params) {
        const url = '/getPostListByUserId';
        const [err, res] = await to(http.get(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async getFollowersList(params) {
        const url = '/getFollowersList';
        const [err, res] = await to(http.get(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async getPostList(params) {
        const url = '/getPostList';
        const [err, res] = await to(http.get(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async getPostCommentList(params) {
        const url = '/getPostCommentList';
        const [err, res] = await to(http.get(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    //8001

    async searchByAddress(params) {
        const url = '/searchByAddress';
        const [err, res] = await to(http.wsapiGet(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async getFriendInfo(params) {
        const url = '/getFriendInfo';
        const [err, res] = await to(http.wsapiGet(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async setAdd(params) {
        const url = '/setAdd';
        const [err, res] = await to(http.wsapiPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async sendFriendAdd(params) {
        const url = '/sendFriendAdd';
        const [err, res] = await to(http.wsapiPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async sendFriendDel(params) {
        const url = '/sendFriendDel';
        const [err, res] = await to(http.wsapiPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async sendFriendAddReply(params) {
        const url = '/sendFriendAddReply';
        const [err, res] = await to(http.wsapiPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async sendFriendText(params) {
        const url = '/sendFriendText';
        const [err, res] = await to(http.wsapiPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async sendFriendRedPacketInApp(params) {
        const url = '/sendFriendRedPacketInApp';
        const [err, res] = await to(http.wsapiPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async sendFriendGrabRedPacket(params) {
        const url = '/sendFriendGrabRedPacket';
        const [err, res] = await to(http.wsapiPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async getRedPacketInfo(params) {
        const url = '/getRedPacketInfo';
        const [err, res] = await to(http.wsapiGet(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async getRedPacketItemList(params) {
        const url = '/getRedPacketItemList';
        const [err, res] = await to(http.wsapiGet(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async sendFriendFile(params) {
        const url = '/sendFriendFile';
        const [err, res] = await to(http.wsapiPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async sendFriendContactCard(params) {
        const url = '/sendFriendContactCard';
        const [err, res] = await to(http.wsapiPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async setFriendMsgSeq(params) {
        const url = '/setFriendMsgSeq';
        const [err, res] = await to(http.wsapiPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async setFriendSeq(params) {
        const url = '/setFriendSeq';
        const [err, res] = await to(http.wsapiPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },


    async getFriendList(params) {
        const url = '/getFriendList';
        const [err, res] = await to(http.wsapiGet(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async getFriendMsg(params) {
        const url = '/getFriendMsg';
        const [err, res] = await to(http.wsapiGet(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async getAddFriendList(params) {
        const url = '/getAddFriendList';
        const [err, res] = await to(http.wsapiGet(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async getFriendMsgSeq() {
        const url = '/getFriendMsgSeq';
        const [err, res] = await to(http.wsapiGet(url, {}));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async sendGroupText(params) {
        const url = '/sendGroupText';
        const [err, res] = await to(http.wsapiPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async sendGroupFile(params) {
        const url = '/sendGroupFile';
        const [err, res] = await to(http.wsapiPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async sendGroupCreate(params) {
        const url = '/sendGroupCreate';
        const [err, res] = await to(http.wsapiPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async setAllowJoin(params) {
        const url = '/setAllowJoin';
        const [err, res] = await to(http.wsapiPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async sendGroupRemoveSelf(params) {
        const url = '/sendGroupRemoveSelf';
        const [err, res] = await to(http.wsapiPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async sendGroupDisband(params) {
        const url = '/sendGroupDisband';
        const [err, res] = await to(http.wsapiPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async sendGroupJoin(params) {
        const url = '/sendGroupJoin';
        const [err, res] = await to(http.wsapiPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },



    async setGroupSeq(params) {
        const url = '/setGroupSeq';
        const [err, res] = await to(http.wsapiPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async sendGroupJoinReply(params) {
        const url = '/sendGroupJoinReply';
        const [err, res] = await to(http.wsapiPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async updateGroupInfo(params) {
        const url = '/updateGroupInfo';
        const [err, res] = await to(http.wsapiPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async setAllowSend(params) {
        const url = '/setAllowSend';
        const [err, res] = await to(http.wsapiPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },


    async sendGroupRemove(params) {
        const url = '/sendGroupRemove';
        const [err, res] = await to(http.wsapiPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async sendGroupInvite(params) {
        const url = '/sendGroupInvite';
        const [err, res] = await to(http.wsapiPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },


    async sendGroupAddAdmin(params) {
        const url = '/sendGroupAddAdmin';
        const [err, res] = await to(http.wsapiPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },


    async sendGroupRemoveAdmin(params) {
        const url = '/sendGroupRemoveAdmin';
        const [err, res] = await to(http.wsapiPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },



    async getGroupAdmins(params) {
        const url = '/getGroupAdmins';
        const [err, res] = await to(http.wsapiGet(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async getGroupMsg(params) {
        const url = '/getGroupMsg';
        const [err, res] = await to(http.wsapiGet(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },


    async getGroupMembers(params) {
        const url = '/getGroupMembers';
        const [err, res] = await to(http.wsapiGet(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async getGroupList(params) {
        const url = '/getGroupList';
        const [err, res] = await to(http.wsapiGet(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async getGroupInfo(params) {
        const url = '/getGroupInfo';
        const [err, res] = await to(http.wsapiGet(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async getHotGroupList(params) {
        const url = '/getHotGroupList';
        const [err, res] = await to(http.wsapiGet(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },

    async fileExist(params) {
        const url = '/fileExist';
        const [err, res] = await to(http.uploadPost(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },


    async uploadMinio(params) {
        const url = '/uploadMinio';
        const [err, res] = await to(http.upload(url, params));
        if (err) {
            return returnErr(err)
        }
        return res;
    },





}
export default httpApi; 
